<?php
require('includes/top.php');

foreach($_POST as $key => $value)
{
if($value!="" && $value!="Submit" && $key!="laptop_model1" && $key!="laptop_model")
{
$data = $key.'_'.$value;

$approved_laptops_array = explode('_',$data);
$approved_id = $approved_laptops_array[1];
$user_id = $approved_laptops_array[2];
$date = date("d-m-y");

$sql="INSERT INTO approved_laptops(`laptop_id`,`user_id`,`date`,`user_name`) VALUES ('$approved_id','$user_id','$date','".$_SESSION['user_name']."')";

$query = mysql_query("SELECT laptopmodel,serial_no,depot_location FROM laptop where user_id = '".trim($user_id)."'");
$queryresult = mysql_fetch_array($query);

$locquery = mysql_query("SELECT user_email_address FROM users where user_address = '".trim($queryresult['depot_location'])."'");
$locqueryresult = mysql_fetch_array($locquery);


// recipients
$to  = $locqueryresult['user_email_address'];
// subject
$subject = 'Login Details';

// message
$message = '
<html>
<head>
  <title>Laptrak</title>
</head>
<body>
  <p><strong>Following laptops are pending to be dispatched:</strong></p>
  <table>
    <tr>
      <td>Model No :</td><td>'.$queryresult['laptopmodel'].'</td>
    </tr>
    <tr>
      <td>Serial No :</td><td>'.$queryresult['serial_no'].'</td>
    </tr>
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To:' . $to . "\r\n";
	$headers .= 'From: no-reply@laptrak.com' . "\r\n";

	// Mail it
	mail($to, $subject, $message, $headers);

$result=mysql_query($sql);

$sql1="UPDATE laptop SET `status` = 'approve' WHERE laptop_id = '".trim($approved_id)."' and user_id = '".trim($user_id)."'";
$result1=mysql_query($sql1); 
$lap_model = GetLapModel($approved_id);
$lap_serno = GetLapSerial($approved_id);
$lap_dlocation = GetLapDLoc($approved_id);


if(GetLaptopDepotNameAPP($lap_model,$lap_serno)== $lap_dlocation)
{
$sql2="UPDATE laptop SET `assigned` = '1',`recived` = '1' WHERE laptop_id = '".trim($approved_id)."' and user_id = '".trim($user_id)."'";
$result2=mysql_query($sql2);
}

}
}
if($result>0)
{
header('Location:view_request_main.php?v=success');
}
else
{
header('Location:view_request_main.php?v=err');
}
?>